package com.zhl.media.app.mvp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhl.media.app.R;
import com.zhl.media.commonsdk.entity.Hm;

import java.util.List;

public class HmListViewAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<Hm> mData;
    public HmListViewAdapter(Context context,List<Hm> data) {
        this.mContext = context;
        this.mData = data;
    }

//    public String[] groupString = {"射手", "辅助", "坦克", "法师"};
//    public String[][] childString = {
//            {"孙尚香", "后羿", "马可波罗", "狄仁杰"},
//            {"孙膑", "蔡文姬", "鬼谷子", "杨玉环"},
//            {"张飞", "廉颇", "牛魔", "项羽"},
//            {"诸葛亮", "王昭君", "安琪拉", "干将"}
//
//    };

    @Override
    // 获取分组的个数
    public int getGroupCount() {
        return mData.size();
    }

    //获取指定分组中的子选项的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(groupPosition).getProjects().size();
    }

    //        获取指定的分组数据
    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    //获取指定分组中的指定子选项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).getProjects().get(childPosition);
    }

    //获取指定分组的ID, 这个ID必须是唯一的
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子选项的ID, 这个ID必须是唯一的
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }
    // 获取显示指定分组的视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hm_list_item,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView)convertView.findViewById(R.id.name);
            groupViewHolder.zl = (TextView)convertView.findViewById(R.id.zl);
            groupViewHolder.ph = (TextView)convertView.findViewById(R.id.ph);
            groupViewHolder.th = (TextView)convertView.findViewById(R.id.th);
            groupViewHolder.groupImg = (ImageView) convertView.findViewById(R.id.group_img);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        groupViewHolder.tvTitle.setText(mData.get(groupPosition).getName());
        groupViewHolder.zl.setText(mData.get(groupPosition).getZl());
        groupViewHolder.ph.setText(mData.get(groupPosition).getPh());
        groupViewHolder.th.setText(mData.get(groupPosition).getTh());
        if (isExpanded){
            groupViewHolder.groupImg.setImageResource(R.drawable.down);
        }
        else{
            groupViewHolder.groupImg.setImageResource(R.drawable.left);
        }
        return convertView;
    }
    /**
     *
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     */

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hm_item_child,parent,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvTitle = (TextView)convertView.findViewById(R.id.name);
            childViewHolder.zl = (TextView)convertView.findViewById(R.id.zl);
            childViewHolder.ph = (TextView)convertView.findViewById(R.id.ph);
            childViewHolder.th = (TextView)convertView.findViewById(R.id.th);
            convertView.setTag(childViewHolder);

        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.tvTitle.setText(mData.get(groupPosition).getProjects().get(childPosition).getName());
        childViewHolder.zl.setText(mData.get(groupPosition).getProjects().get(childPosition).getZl());
        childViewHolder.ph.setText(mData.get(groupPosition).getProjects().get(childPosition).getPh());
        childViewHolder.th.setText(mData.get(groupPosition).getProjects().get(childPosition).getTh());
        return convertView;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tvTitle;
        TextView zl;
        TextView ph;
        TextView th;
        ImageView groupImg;
    }

    static class ChildViewHolder {
        TextView tvTitle;
        TextView zl;
        TextView ph;
        TextView th;
    }

}
